<template>
  <Dialog v-model="dialogVisible" title="公告详情" width="60%">
    <el-descriptions
      v-loading="formLoading"
      :column="2"
      border
      label-width="100px"
    >
      <el-descriptions-item label="公告编号">
        {{ formData.id || '' }}
      </el-descriptions-item>
      <el-descriptions-item label="公告标题">
        {{ formData.title }}
      </el-descriptions-item>
      <el-descriptions-item label="公告类型">
        <dict-tag :type="DICT_TYPE.SYSTEM_NOTICE_TYPE" :value="formData.type || 0" />
      </el-descriptions-item>
      <!-- <el-descriptions-item label="状态">
        <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="formData.status || 0" />
      </el-descriptions-item> -->
      <el-descriptions-item label="发布时间">
        {{ formData.createTime ? formatDate(new Date(formData.createTime)) : '' }}
      </el-descriptions-item>
      <el-descriptions-item label="发布部门">
        {{ formData.deptName }}
      </el-descriptions-item>
      <el-descriptions-item label="接收人" :span="2">
        <template v-if="formData.receiveType === 2">{{ formData.receiverNames }}</template>
        <template v-else>全员</template>
      </el-descriptions-item>
      
      <el-descriptions-item label="公告内容" :span="2">
        <div v-html="formData.content" class="notice-content"></div>
      </el-descriptions-item>
      <el-descriptions-item label="附件" :span="2" v-if="formData.files">
        <OaFileManager :header="false" :initialFileIds="formData.files" />
      </el-descriptions-item>
      <el-descriptions-item label="备注" :span="2" v-if="formData.remark">
        {{ formData.remark }}
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import * as NoticeApi from '@/api/system/notice'
import OaFileManager from '@/components/Oa/OaFileManager.vue'

defineOptions({ name: 'NoticeDetail' })

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const formData = ref({
  id: undefined as number | undefined,
  title: '',
  type: undefined as number | undefined,
  content: '',
  status: undefined as number | undefined,
  remark: '',
  receiverIds: '',
  receiverNames: '',
  deptId: undefined as number | undefined,
  receiveType: 1,
  deptName: '',
  files: '',
  createTime: undefined as string | undefined
})

/** 打开弹窗 */
const open = async (id: number) => {
  dialogVisible.value = true
  formLoading.value = true
  try {
    const data = await NoticeApi.getNotice(id)
    formData.value = {
      ...data,
      files: data.files || '',
      receiverIds: data.receiverIds || '',
      receiverNames: data.receiverNames || '',
      deptName: data.deptName || ''
    }
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style scoped>
.notice-content {
  max-height: 300px;
  overflow-y: auto;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background-color: #fafafa;
}
</style> 